#pragma warning(disable : 4996)
#include"SL.h"
STnode* STbuynode(STDateType x)
{
	STnode* node = (STnode*)malloc(sizeof(STnode));
	assert(node != NULL);
	node->x = x;
	node->next = NULL;
	return node;
}
void SLprint(STnode* head)
{
	STnode* tail = head;
	if (head != NULL)
	{
		while (tail)
		{
			printf("%d->", tail->x);
			tail = tail->next;
		}
	}
	printf("NULL\n");
}
void SLpushback(STnode** hhead, STDateType x)
{
	assert(hhead);
	if ((*hhead) == NULL)
	{
		(*hhead) = STbuynode(x);
		return;

	}
	STnode* tail = *hhead;
	while (tail->next)
	{
		tail = tail->next;
	}
	tail->next = STbuynode(x);


}
void SLpopback(STnode** hhead)
{
	assert(*hhead);
	STnode*tail =*hhead;
	STnode* pre = NULL;
	if (tail->next == NULL)
	{
		free(tail);
		*hhead = NULL;
	}
	else
	{
		while ((tail)->next)
		{
			pre = tail;
			(tail) = (tail)->next;
		}
		free(tail);
		pre->next = NULL;
	}
	
}
void SLpushfront(STnode** hhead,STDateType x)
{

	STnode* new = STbuynode(x);
	new->next = *hhead;
	*hhead = new;
}
void SLpopfront(STnode** hhead)
{
	assert(*hhead);
	STnode* tail = (*hhead)->next;
	free(*hhead);
	*hhead = tail;



}
STnode* SLfind(STnode* hhead, STDateType x)
{
	while (hhead->x != x)
	{
		if (hhead->next == NULL)
		{
			return NULL;
		}
		hhead = hhead->next;
	}
	return hhead;


}
void SLInsertAfter(STnode* pos, STDateType x)
{
	STnode* tail = STbuynode(x);
	STnode* temp = pos->next;
	pos->next = tail;
	tail->next = temp;
}
void SLDestoryAfter(STnode* pos)
{
	assert(pos->next != NULL);
	STnode* temp =pos->next->next;
	free(pos->next);
	pos->next = temp;;


}
void SLDestory(STnode** head)
{
	STnode* tail = *head;
	while (tail)
	{
		tail = (*head)->next;
		free(*head);
		*head = tail;
	}
}